import EventEmitter from '@/lib/core/EventEmitter';
/**
 * 键盘工具
 */
export default class KeyboardUtils {
    private static instance: KeyboardUtils;
    public static getInstance() {
        if (!this.instance) {
            this.instance = new KeyboardUtils();
        }
        return this.instance;
    }
    // 原始窗口高度，android用到
    private windowHeight = window.innerHeight;

    private KeyboardUtils() {

    }
    /**
     * 监听键盘弹起隐藏状态改变过
     * @param callback 
     */
    public setOnChangeListener(callback: (keyboardHeight: number) => void) {
        if (api.systemType === 'ios') {
            EventEmitter.onSystem({
                name: 'keyboardhide' as any
            }, () => {
                callback(0);
            });
            EventEmitter.onSystem({
                name: 'keyboardshow' as any
            }, (ret) => {
                const h = ret.h;
                callback(h);
            });
        } else {
            window.addEventListener('resize', () => {
                const h = this.windowHeight - window.innerHeight;
                callback(h);
            }, false);
        }
    }

}
